 //
//  TextFieldDemo.swift
//  SwiftUIDemo
//
//  Created by Tilt Wang on 2025/6/30.
//

import SwiftUI

struct TextFieldDemo: View {
    
    @State var textFieldText: String = ""
    @State var dataArray: [String] = []
    
    var body: some View {
        NavigationView {
            VStack {
                TextField("Type something here...", text: $textFieldText)
                    .padding()
                    .background(Color.gray.opacity(0.3)).cornerRadius(10)
                    .foregroundColor(.red)
                    .font(.headline)
                
                Button {
                    print(textFieldText)
                    if textIsAppropriate() {
                        saveText()
                    }
                } label: {
                    Text("print result")
                        .padding()
                        .frame(maxWidth: .infinity)
                        .background(textIsAppropriate() ? Color.blue : Color.gray)
                        .cornerRadius(10)
                        .foregroundColor(.white)
                        .font(.headline)
                }
                .disabled(!textIsAppropriate())
                ForEach(dataArray, id: \.self) { data in
                    Text(data)
                        .padding(.leading)
                
                }
                Spacer()
            }
            .padding()
            .navigationTitle("TextField Demo")
        }

    }
    
    func textIsAppropriate() -> Bool {
        return textFieldText.count >= 3
    }
    
    func saveText() {
        dataArray.append(textFieldText)
        textFieldText = ""
    }
    
}

#Preview {
    TextFieldDemo()
}
